package cn.sixmillions.yuqueblog.config;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

/**
 * @author sixmillions
 * @description Swagger3配置
 * @createDate 2021-12-10
 * swagger3默认接口地址：http://localhost:8080/swagger-ui/index.html
 * Knife4j文档默认地址：http://localhost:8080/doc.html
 * Springfox使用的路径匹配是基于AntPathMatcher的，Spring Boot 2.6.X使用的是PathPatternMatcher，
 * Spring Boot 2.6.X需要配置spring.mvc.pathmatch.matching-strategy: ANT_PATH_MATCHER
 */
@Configuration
@EnableKnife4j
@ConditionalOnProperty(value = "springfox.documentation.enabled", havingValue = "true", matchIfMissing = true)
@Slf4j
public class Swagger3Config {

    @Value("${swagger.title:Swagger3接口文档}")
    private String title;

    @Value("${swagger.description:project-description}")
    private String description;

    @Value("${swagger.author:unknow}")
    private String author;

    @Value("${swagger.website:url}")
    private String website;

    @Value("${swagger.email:xxxx@test.com}")
    private String email;

    @Value("${swagger.version:1.0.0}")
    private String version;

    @Bean
    public Docket createRestApi() {
        log.info("加载Swagger3相关配置....");
        return new Docket(DocumentationType.OAS_30)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title(title)
                .description(description)
                .contact(new Contact(author, website, email))
                .version(version)
                .build();
    }
}
